
from rest_framework.generics import ListAPIView #不用分页
from .models import CourseCategory
from .serializers import CourseCategoryModelSerializer
class CourseCategoryListAPIView(ListAPIView):
    """课程分类接口"""
    queryset = CourseCategory.objects.filter(is_show=True,is_deleted=False).order_by("orders","-id")# 根据id进行倒序
    serializer_class = CourseCategoryModelSerializer

# from .models import Course
# from .serializers import CourseModelSerializer
# class CourseListAPIView(ListAPIView):
#     """弃用：课程基本信息接口，没有提供课程列表升降序排列"""
#     queryset = Course.objects.filter(is_show=True,is_deleted=False).order_by("orders","-id")# 根据id进行倒序
#     serializer_class = CourseModelSerializer


from .models import Course
from .serializers import CourseModelSerializer
from django_filters.rest_framework import DjangoFilterBackend   # 引入分类过滤器 例如当某个字段都为x时的过滤
from rest_framework.filters import OrderingFilter   # 引入排序过滤器 正序倒序等
from .paginations import CustomPageNumberPagination # 引入分页组件


class CourseListAPIView(ListAPIView):
    """课程基本信息接口，提供课程列表升降序排列功能"""
    queryset = Course.objects.filter(is_show=True,is_deleted=False).order_by("orders","-id")# 根据id进行倒序
    serializer_class = CourseModelSerializer
    filter_backends = [DjangoFilterBackend,OrderingFilter, ]
    filter_fields = ['course_category',]    #对序列化器 引用的 Course 表中的 course_category 字段进行过滤，这个方法类似前端请求 http://api.luffycity.cn:8000/course/?course_category=1  当course_category的id为1的数据
    ordering_fields = ('id', 'students', 'price') #对Course表的id，students，price字段进行升降排序，这个方法类似前端请求 http://api.luffycity.cn:8000/course/?course_category=2&ordering=price  ordering=price对price字段进行排序，如果倒序就用 -price
    pagination_class = CustomPageNumberPagination   #分页后所有数据结构有变化，需要前端重新根据接口获取数据




from rest_framework.generics import RetrieveAPIView
from .serializers import CourseRetrieveSerializer
class CourseRetrieveAPIView(RetrieveAPIView):
    """
        http://api.luffycity.cn:8000/course/1

    """
    queryset = Course.objects.filter(is_show=True,is_deleted=False).order_by("orders","-id")# 根据id进行倒序
    serializer_class = CourseRetrieveSerializer




from .models import CourseChapter
from rest_framework.generics import ListAPIView
from .serializers import CourseChapterSerializer
from django_filters.rest_framework import DjangoFilterBackend   # 引入分类过滤器 例如当某个字段都为x时的过滤
class CourseChapterListAPIView(ListAPIView):
    """
        测试数据有误，需要重新将xadmin中课程章节中每行的"是否显示"重新设置去勾/打钩 保存，将is_show的1改为True
    """
    queryset = CourseChapter.objects.filter(is_show=True,is_deleted=False).order_by("orders","id")
    serializer_class = CourseChapterSerializer
    # 需要过滤
    filter_backends = [DjangoFilterBackend, ]   # 对字段过滤 引入分类过滤器 例如当某个字段都为x时的过滤
    filter_fields = ['course', ]  # CourseChapter下的 course外键字段，这个方法类似前端请求 http://api.luffycity.cn:8000/course/chapter/?course=1  当course的id为1的数据




"""保利威2020-7-31"""
from luffyapi.libs.polyv import PolyvPlayer
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from django.conf import settings
from rest_framework.response import Response

class PolyvAPIView(APIView):
    permission_classes = [IsAuthenticated]  #测试注释掉
    def get(self,request):
        """获取视频播放的token"""
        userId = settings.POLYV_CONFIG["userId"]
        secretKey = settings.POLYV_CONFIG["secretKey"]
        tokenUrl = settings.POLYV_CONFIG["tokenUrl"]
        #实例化保利威sdk
        polyv = PolyvPlayer(userId,secretKey,tokenUrl)

        vid = request.query_params.get("vid")
        user_addr = request.META.get("REMOTE_ADDR")  # 用户的IP
        user_id = request.user.id      # 用户ID 测试给1
        user_name = request.user.username  # 用户名，测试给个test什么的
        # print("参数",userId,secretKey,tokenUrl,vid,user_addr,user_id,user_name)
        result = polyv.get_video_token(vid , user_addr , user_id , user_name)
        print("请求结果",result)

        return Response(result.get("token"))















